import fang from '../../models/Fang';
import Cache from '../../utils/Cache';
Page({
  data: {
    fang_id: 0,

    latitude: 0,
    longitude: 0,

    markers: [],
    fang: {},
    fav: 0,
    favstr: ['添加收藏','取消收藏'],
  },
  onLoad(options) {
    // 房源id
    let fang_id = options.id;

    this.data.fang_id = fang_id;

    fang.getFangInfo(fang_id).then(ret => {

      let data = ret.data.data;

      let markers = {
        id: data.id,
        latitude: data.latitude,
        longitude: data.longitude,
        title: data.fang_name,
        iconPath: 'https://fang.1314000.cn/zfw/location.png',
      }

      let latitude = data.latitude;
      let longitude = data.longitude;

      this.setData({
        fang_id,
        latitude,
        longitude,
        markers: [ ...this.data.markers, markers ],
        fang:data
      });
    });

    fang.isFav(fang_id).then( ret => {
      this.setData({
        fav: ret.data.data
      });
    });
  },
  onReady() {
    fang.addRec(this.data.fang_id);
  },
  onShareAppMessage() {
    let id = this.data.fang.id;

    let rid = Cache.get('id');
    return {
      title: this.data.fang.fang_name,
      path: `/pages/fang/fang?id=${id}&rid=${rid}`,
      imageUrl: this.data.fang.pic_url
    }
  },
  callPhone(evt){
    let phoneNumber = evt.target.dataset.phone;
    wx.makePhoneCall({
      phoneNumber
    });
  },
  // 添加收藏
  addFav() {
    let fav = this.data.fav;

    let fang_id = this.data.fang_id;

    fang.addFav(fang_id,fav).then( ret => {
      this.setData({
        fav: fav == 0 ? 1 : 0
      });
    });
  }
})